package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.comment.response.JsonResult;
import cn.tedu._05weibo.comment.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDTO;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

import java.util.Date;

@Api(tags = "01.用户管理模块")
@Slf4j
@RestController
@RequestMapping(value = "/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.注册功能
     */
    @ApiOperation(value = "1.注册功能")
    @PostMapping("reg")
    public JsonResult register(@RequestBody @Validated UserRegDTO userRegDTO) {
        log.debug("userRegDTO = " + userRegDTO);

        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        // 判断用户名是否被占用
        if (userVO != null) {
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        // 注册
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreated(new Date());
        System.out.println("user = " + user);
        userMapper.insert(user);
        return JsonResult.success();
    }


    /**
     * 2.登录功能
     *
     * userVO是数据库查询出来的数据
     * userLoginDTO是前端传过来的数据
     * @ApiIgnore 注解：Swagger忽略该参数
     */
    @ApiOperation(value = "2.登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO , @ApiIgnore HttpSession session) {
        log.debug("userLoginDTO = " + userLoginDTO);

        //校验用户名是否正确
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        //用户名错误
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }
        //用户名正确,开始校验密码是否正确,登录成功
        if (userLoginDTO.getPassword().equals(userVO.getPassword())) {
            //登录成功，将用户信息保存到session中
            session.setAttribute("user", userVO);
            return new JsonResult(StatusCode.LOGIN_SUCCESS);
        }
        //密码错误
        return new JsonResult(StatusCode.PASSWORD_ERROR);
    }

    /**
     * 3.获取当前用户
     */
    @ApiOperation(value = "3.获取当前用户")
    @GetMapping("currentUser")
    public JsonResult getCurrentUser(@ApiIgnore HttpSession session) {
        return JsonResult.success((UserVO) session.getAttribute("user"));
    }

    /**
     * 4.退出登录
     */
    @ApiOperation(value = "4.退出登录")
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session) {
        //移除session
        session.removeAttribute("user");
    }

}
